package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.base.api.validation.ValidationGroup;
import cn.aws360.boot.system.api.pojo.request.SysUserPasswordWrite;
import cn.aws360.boot.system.api.pojo.request.SysUserProfileWrite;
import cn.aws360.boot.system.biz.service.SysUserService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiSupport(order = MODULE_HOME_CODE)
@Api(value = MODULE_HOME_NAME, tags = MODULE_HOME_TAGS)
public class HomeController {

    private final SysUserService userService;

    @SaCheckLogin
    @GetMapping("/home/*.json")
    @ApiOperation(value = "主页页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @SaCheckLogin
    @GetMapping("/home/main")
    @ApiOperation(value = "首页信息")
    @ApiOperationSupport(order = 1)
    public ResultModel<Map<String, Object>> list() {
        Map<String, Object> mainMap = new HashMap<>(8);
        return ResultModel.ok(mainMap);
    }

    @SaCheckLogin
    @PutMapping("/person/avatar")
    @OpsLog(title = "修改个人信息", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改个人头像")
    @ApiOperationSupport(order = 21)
    public ResultModel<Map<String, String>> editAvatar(@RequestPart("file") MultipartFile multipartFile) {
        return ResultModel.ok(MapUtil.of("value", userService.editAvatar(multipartFile)));
    }

    @SaCheckLogin
    @PutMapping("/person/profile")
    @OpsLog(title = "修改个人信息", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改个人信息")
    @ApiOperationSupport(order = 22)
    public ResultModel<Boolean> editProfile(@Validated(ValidationGroup.edit.class) @RequestBody SysUserProfileWrite sysUserProfileWrite) {
        return ResultModel.ok(userService.editProfile(sysUserProfileWrite));
    }

    @SaCheckLogin
    @PutMapping("/person/password")
    @OpsLog(title = "修改个人密码", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改个人密码")
    @ApiOperationSupport(order = 23)
    public ResultModel<Boolean> editPassword(@Validated(ValidationGroup.edit.class) @RequestBody SysUserPasswordWrite sysUserPasswordWrite) {
        return ResultModel.ok(userService.editPassword(sysUserPasswordWrite));
    }

}
